package yang.cn.com.greendaoapp;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import yang.cn.com.greendaoapp.adapter.MultipleItem;
import yang.cn.com.greendaoapp.adapter.MultipleItemQuickAdapter;
import yang.cn.com.greendaoapp.base.BaseActivity;
import yang.cn.com.greendaoapp.bean.EventCenter;
import yang.cn.com.greendaoapp.bean.FileDao;
import yang.cn.com.greendaoapp.bean.FileInfo;
import yang.cn.com.greendaoapp.utils.FileUtil;
import yang.cn.com.greendaoapp.view.CheckBox;
import yang.cn.com.greendaoapp.view.DividerItemDecoration;

import static yang.cn.com.greendaoapp.utils.FileUtil.fileFilter;
import static yang.cn.com.greendaoapp.utils.FileUtil.getFileInfosFromFileArray;

/**
 * Created by Administrator on 2017/5/27.
 */

public class SDCardActivity extends BaseActivity {
    @Bind(R.id.iv_title_back)
    ImageView ivTitleBack;
    @Bind(R.id.tv_title_middle)
    TextView tvTitleMiddle;
    @Bind(R.id.tv_path)
    TextView tvPath;
    @Bind(R.id.rlv_sd_card)
    RecyclerView rlvSdCard;
    @Bind(R.id.tv_all_size)
    TextView tvAllSize;
    @Bind(R.id.tv_send)
    TextView tvSend;
    @Bind(R.id.activity_sdcard)
    LinearLayout activitySdcard;


    private List<FileInfo> fileInfos = new ArrayList<>();
    private List<MultipleItem> mMultipleItems = new ArrayList<>();
    private MultipleItemQuickAdapter mAdapter;
    private File mCurrentPathFile = null;
    private File mSDCardPath = null;
    private String path;
    @Override
    public void onEventComming(EventCenter var1) {

    }

    @Override
    public boolean isBindEventBusHere() {
        return false;
    }

    @Override
    public void initViewAndEvent() {
        tvAllSize.setText(getString(R.string.size, "0B"));
        tvSend.setText(getString(R.string.send, "0"));
        path = getIntent().getStringExtra("path");
        tvTitleMiddle.setText(getIntent().getStringExtra("name"));
        mSDCardPath = new File(path);
        rlvSdCard.setLayoutManager(new LinearLayoutManager(this));
        rlvSdCard.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL, R.drawable.divide_line));
        mAdapter = new MultipleItemQuickAdapter(mMultipleItems);
        rlvSdCard.setAdapter(mAdapter);
        showFiles(mSDCardPath);
        updateSizAndCount();
        rlvSdCard.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {

                if (adapter.getItemViewType(position) == MultipleItem.FILE) {
                    boolean isCheck = fileInfos.get(position).getIsCheck();
                    fileInfos.get(position).setIsCheck(!isCheck);
                    if (fileInfos.get(position).getIsCheck()) {
                        FileDao.insertFile(fileInfos.get(position));
                        ((CheckBox) view.findViewById(R.id.cb_file)).setChecked(true, true);
                    } else {
                        FileDao.deleteFile(fileInfos.get(position));
                        ((CheckBox) view.findViewById(R.id.cb_file)).setChecked(false, true);
                    }
                    EventBus.getDefault().post(new EventCenter<>(3));
                    updateSizAndCount();
                } else {
                    showFiles(new File(fileInfos.get(position).getFilePath()));
                }

            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_sdcard;
    }
    public void updateSizAndCount() {
        List<FileInfo> mList = FileDao.queryAll();
        if (mList.size() == 0) {
            tvSend.setBackgroundResource(R.drawable.shape_bt_send);
            tvSend.setTextColor(getResources().getColor(R.color.md_grey_700));
            tvAllSize.setText(getString(R.string.size, "0B"));
        } else {
            tvSend.setBackgroundResource(R.drawable.shape_bt_send_blue);
            tvSend.setTextColor(getResources().getColor(R.color.md_white_1000));
            long count = 0L;
            for (int i = 0; i < mList.size(); i++) {
                count = count + mList.get(i).getFileSize();
            }
            tvAllSize.setText(getString(R.string.size, FileUtil.FormetFileSize(count)));
        }
        tvSend.setText(getString(R.string.send, "" + mList.size()));
    }
    private void showFiles(File folder) {
        mMultipleItems.clear();
        tvPath.setText(folder.getAbsolutePath());
        mCurrentPathFile = folder;
        File[] files = fileFilter(folder);
        if (null == files || files.length == 0) {
            mAdapter.setEmptyView(getEmptyView());
            Log.e("files", "files::为空啦");
        } else {
            //获取文件信息
            fileInfos = getFileInfosFromFileArray(files);
            for (int i = 0; i < fileInfos.size(); i++) {
                if (fileInfos.get(i).isDirectory) {
                    mMultipleItems.add(new MultipleItem(MultipleItem.FOLD, fileInfos.get(i)));
                } else {
                    mMultipleItems.add(new MultipleItem(MultipleItem.FILE, fileInfos.get(i)));
                }

            }
            //查询本地数据库，如果之前有选择的就显示打钩
            List<FileInfo> mList = FileDao.queryAll();
            for (int i = 0; i < fileInfos.size(); i++) {
                for (FileInfo fileInfo : mList) {
                    if (fileInfo.getFileName().equals(fileInfos.get(i).getFileName())) {
                        fileInfos.get(i).setIsCheck(true);
                    }
                }
            }
        }
        mAdapter.notifyDataSetChanged();
    }

    private View getEmptyView() {
        return getLayoutInflater().inflate(R.layout.empty_view, (ViewGroup) rlvSdCard.getParent(), false);
    }

}
